<?php

/**
 * This is the model class for table "tb_bitacora".
 *
 * The followings are the available columns in table 'tb_bitacora':
 * @property integer $idbitacora
 * @property string $fecha
 * @property string $hora
 * @property string $descripcion
 * @property string $tb_usuarios_login
 * @property integer $tb_tipoaccion_idtb_tipoaccion
 *
 * The followings are the available model relations:
 * @property TbUsuarios $tbUsuariosLogin
 * @property TbTipoaccion $tbTipoaccionIdtbTipoaccion
 */
class TbBitacora extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return TbBitacora the static model class
	 */
	public $accionActual;
	public $fechaActual;
	public $usuarioActual;
	public function obtenerUsuarios(){
		return (CHtml::listData(TbUsuarios::model()->findAll(),'login','nombreCompleto'));
	}
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tb_bitacora';
	}

	public function asignarDateActual(){
		$this->fecha = Yii::app()->db->createCommand('SELECT curdate()')->queryScalar();
		$this->hora = Yii::app()->db->createCommand('SELECT curtime()')->queryScalar();
	}
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('fecha, hora, descripcion, tb_usuarios_login, tb_tipoaccion_idtb_tipoaccion', 'required'),
			array('tb_tipoaccion_idtb_tipoaccion', 'numerical', 'integerOnly'=>true),
			array('descripcion', 'length', 'max'=>200),
			array('tb_usuarios_login', 'length', 'max'=>50),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idbitacora, fecha, hora, descripcion, tb_usuarios_login, tb_tipoaccion_idtb_tipoaccion', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'tbUsuariosLogin' => array(self::BELONGS_TO, 'TbUsuarios', 'tb_usuarios_login'),
			'tbTipoaccionIdtbTipoaccion' => array(self::BELONGS_TO, 'TbTipoaccion', 'tb_tipoaccion_idtb_tipoaccion'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idbitacora' => 'Id',
			'fecha' => 'Fecha',
			'hora' => 'Hora',
			'descripcion' => 'Descripcion',
			'tb_usuarios_login' => 'Usuario',
			'tb_tipoaccion_idtb_tipoaccion' => 'Accion',
			'accionActual'=>'Accion',
			'fechaActual'=>'Fecha',
			'usuarioActual'=>'Usuario',
		);
	}
	public function obtenerAcciones(){
		return (CHtml::listData(TbTipoaccion::model()->findAll(),'idtb_tipoaccion','accion'));
	}
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idbitacora',$this->idbitacora);
		$criteria->compare('fecha',$this->fecha,true);
		$criteria->compare('hora',$this->hora,true);
		$criteria->compare('descripcion',$this->descripcion,true);
		$criteria->compare('tb_usuarios_login',$this->tb_usuarios_login,true);
		$criteria->compare('tb_tipoaccion_idtb_tipoaccion',$this->tb_tipoaccion_idtb_tipoaccion);
		//$criteria->compare('accionActual',$this->accionActual);
		$criteria->order='fecha DESC, hora DESC';
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	protected function afterFind(){
		parent::afterFind();
		$acciones = TbTipoaccion::model()->findByPk($this->tb_tipoaccion_idtb_tipoaccion);
		$this->accionActual = $acciones->accion;
		$h = new TbUtility();
		$this->fechaActual = $h->AcomodarFormatoFecha($this->fecha);		
		$usu = TbUsuarios::model()->findByPk($this->tb_usuarios_login);
		$this->usuarioActual = $usu->nombre." ".$usu->apellido;
	}
	public function asignarAccion($idA){
		$acciones = TbTipoaccion::model()->findByAttributes(array('accion'=>$idA));
		$this->tb_tipoaccion_idtb_tipoaccion = $acciones->idtb_tipoaccion;	
			
	}
}